import { createRouter, createWebHashHistory } from "vue-router";
import store from "../store/index";
const routes = [
  {
    path: "/",
    name: "home",
    meta: {
      isGuard: true, //是否需要守卫  true需要
    },
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/HomeView.vue"),
  },
  {
    path: "/login",
    name: "login",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/LoginView.vue"),
  },
  {
    meta: {
      isGuard: true, //是否需要守卫  true需要
    },
    path: "/userList",
    name: "userList",
    component: () => import("../views/UserList.vue"),
  },
  {
    meta: {
      isGuard: true, //是否需要守卫  true需要
    },
    path: "/userClassfiy",
    name: "userClassfiy",
    component: () => import("../views/role/UserClassfiy.vue"),
  },
  {
    meta: {
      isGuard: true, //是否需要守卫  true需要
    },
    path: "/roleList",
    name: "roleList",
    component: () => import("../views/role/RoleListView.vue"),
  },
  {
    meta: {
      isGuard: true, //是否需要守卫  true需要
    },
    path: "/commList",
    name: "commList",
    component: () => import("../views/commodity/CommListView.vue"),
  },
  {
    meta: {
      isGuard: true, //是否需要守卫  true需要
    },
    path: "/addGoods",
    name: "addGoods",
    component: () => import("../views/commodity/AddGoods.vue"),
  },
  {
    meta: {
      isGuard: true, //是否需要守卫  true需要
    },
    path: "/attrClassfiy",
    name: "attrClassfiy",
    component: () => import("../views/commodity/AttrClassfiy.vue"),
  },
  {
    meta: {
      isGuard: true, //是否需要守卫  true需要
    },
    path: "/goodsClassfiy",
    name: "goodsClassfiy",
    component: () => import("../views/commodity/GoodsClassify.vue"),
  },
  {
    meta: {
      isGuard: true, //是否需要守卫  true需要
    },
    path: "/orderList",
    name: "orderList",
    component: () => import("../views/OrderList.vue"),
  },
  {
    meta: {
      isGuard: true, //是否需要守卫  true需要
    },
    path: "/report",
    name: "report",
    component: () => import("../views/ReportView.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
router.beforeEach((to) => {
  let { meta, path, name } = to;
  if (meta.isGuard) {
    if (!store.state.token) {
      return `/login?toPath${path}&toName=${name}`;
    }
  }
});
export default router;
